package com.ktjy.ruoyidata.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Random;

import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ktjy.ruoyidata.mapper.MyVendorMapper;
import com.ktjy.ruoyidata.domain.MyVendor;
import com.ktjy.ruoyidata.service.IMyVendorService;

/**
 * 供应商Service业务层处理
 *
 * @author t382
 * @date 2025-05-06
 */
@Service
public class MyVendorServiceImpl implements IMyVendorService {
    @Autowired
    private MyVendorMapper myVendorMapper;

    /**
     * 查询供应商
     *
     * @param vendorId 供应商主键
     * @return 供应商
     */
    @Override
    public MyVendor selectMyVendorByVendorId(Long vendorId) {
        return myVendorMapper.selectMyVendorByVendorId(vendorId);
    }

    /**
     * 查询供应商列表
     *
     * @param myVendor 供应商
     * @return 供应商
     */
    @Override
    public List<MyVendor> selectMyVendorList(MyVendor myVendor) {
        return myVendorMapper.selectMyVendorList(myVendor);
    }

    /**
     * 新增供应商
     *
     * @param myVendor 供应商
     * @return 结果
     */
    @Override
    public int insertMyVendor(MyVendor myVendor) {
        // 生成供应商编码
        String prefix = "V"; // 默认

        SimpleDateFormat sdf = new SimpleDateFormat("yyMMdd");
        String datePart = sdf.format(new Date());

        // 生成4位随机数
        String randomPart = String.format("%04d", new Random().nextInt(10000));

        String clientCode = prefix + datePart + randomPart;
        myVendor.setVendorCode(clientCode);

        myVendor.setCreateTime(DateUtils.getNowDate());
        return myVendorMapper.insertMyVendor(myVendor);
    }

    /**
     * 修改供应商
     *
     * @param myVendor 供应商
     * @return 结果
     */
    @Override
    public int updateMyVendor(MyVendor myVendor) {
        myVendor.setUpdateTime(DateUtils.getNowDate());
        return myVendorMapper.updateMyVendor(myVendor);
    }

    /**
     * 批量删除供应商
     *
     * @param vendorIds 需要删除的供应商主键
     * @return 结果
     */
    @Override
    public int deleteMyVendorByVendorIds(Long[] vendorIds) {
        return myVendorMapper.deleteMyVendorByVendorIds(vendorIds);
    }

    /**
     * 删除供应商信息
     *
     * @param vendorId 供应商主键
     * @return 结果
     */
    @Override
    public int deleteMyVendorByVendorId(Long vendorId) {
        return myVendorMapper.deleteMyVendorByVendorId(vendorId);
    }
}
